FUNCTION YBC_SWS_FORWARD_REQUEST.
*"----------------------------------------------------------------------
*"*"本地接口：
*"  IMPORTING
*"     VALUE(IS_APPROVER) TYPE  YBC_SWS_APPROVER
*"     VALUE(IV_FORWARDAPPRS) TYPE  CHAR200
*"     VALUE(IV_LANGUAGE) TYPE  CHAR10 DEFAULT 'ZH'
*"  EXPORTING
*"     VALUE(ES_MSG) TYPE  BAPIRET2
*"----------------------------------------------------------------------

  DATA:
    LS_USER        TYPE YBC_SWS_USER,
    LV_USERNAME    TYPE TEXT100,
    LS_APPR_USER   TYPE YBC_SWS_USER,
    LV_REMARKS     TYPE YBC_SWS_APPROVER-REMARKS,
    LV_FWDS        TYPE STRING,
    LS_TEMP        TYPE CHAR50,
    LT_TEMP        TYPE STANDARD TABLE OF CHAR50,
    LT_APPROVER    TYPE STANDARD TABLE OF YBCS_SWS_APPROVER WITH HEADER LINE,
    LS_APPROVER_DB TYPE YBC_SWS_APPROVER,
    LT_USER        TYPE STANDARD TABLE OF YBC_SWS_USER,
    LT_APPROVER_DB TYPE STANDARD TABLE OF YBC_SWS_APPROVER.

  IF IV_FORWARDAPPRS IS INITIAL.
    CLEAR ES_MSG.
    ES_MSG-TYPE = 'E'.
    ES_MSG-ID = 'YBC_SWS'.
    ES_MSG-NUMBER = '001'.
    MESSAGE S040(YBC_SWS) INTO ES_MSG-MESSAGE .
    RETURN.
  ENDIF.

  SPLIT IV_FORWARDAPPRS AT ',' INTO TABLE LT_TEMP.
  LOOP AT LT_TEMP INTO LS_TEMP.
    CONDENSE LS_TEMP.
    TRANSLATE LS_TEMP TO UPPER CASE.
    CLEAR LT_APPROVER.
    LT_APPROVER-APPLEVEL = IS_APPROVER-APPLEVEL.
    LT_APPROVER-APPLEVEL_DESC1 = IS_APPROVER-APPLEVEL_DESC1.
    LT_APPROVER-APPLEVEL_DESC2 = IS_APPROVER-APPLEVEL_DESC2.
    LT_APPROVER-APPLEVEL_DESC3 = IS_APPROVER-APPLEVEL_DESC3.
    LT_APPROVER-USERID = LS_TEMP.
    LT_APPROVER-SAPID = LS_TEMP.
*    LT_APPROVER-EXTUSID = LS_TEMP.
    LT_APPROVER-COUNTERSIGN = IS_APPROVER-COUNTERSIGN.

    PERFORM FORM_GET_USER_DATA CHANGING LT_APPROVER.
    IF LV_FWDS IS INITIAL.
      LV_FWDS = LT_APPROVER-NAME1.
    ELSE.
      CONCATENATE LV_FWDS LT_APPROVER-NAME1 INTO LV_FWDS SEPARATED BY ', '.
    ENDIF.

    APPEND LT_APPROVER.
  ENDLOOP.


  "审批者处理
  SELECT *
    INTO TABLE LT_USER
    FROM YBC_SWS_USER
    FOR ALL ENTRIES IN LT_APPROVER
   WHERE EXTUSID = LT_APPROVER-EXTUSID
     AND SAPID = LT_APPROVER-SAPID .

  "检查审批者是否存在，不存在则创建
  LOOP AT LT_APPROVER.
    CLEAR LS_USER.
    IF LT_APPROVER-EXTUSID IS NOT INITIAL.
      READ TABLE LT_USER INTO LS_USER
        WITH KEY EXTUSID = LT_APPROVER-EXTUSID.
    ENDIF.

    IF LS_USER IS INITIAL.
      IF LT_APPROVER-SAPID IS NOT INITIAL.
        READ TABLE LT_USER INTO LS_USER
          WITH KEY SAPID = LT_APPROVER-SAPID.
      ENDIF.
    ENDIF.

    IF LS_USER IS INITIAL.
      MOVE-CORRESPONDING LT_APPROVER TO LS_USER.
      PERFORM FRM_CREATE_USER CHANGING LS_USER ES_MSG.
      IF ES_MSG-TYPE = 'E'.
        EXIT.
      ENDIF.
      "   APPEND LS_USER TO LT_USER.
    ENDIF.

    CLEAR LS_APPROVER_DB.
    LS_APPROVER_DB-REQUESTID = IS_APPROVER-REQUESTID.
    LS_APPROVER_DB-APPROVERID = LS_USER-USERID.
    LS_APPROVER_DB-APPLEVEL = LT_APPROVER-APPLEVEL.
    LS_APPROVER_DB-APPLEVEL_DESC1 = LT_APPROVER-APPLEVEL_DESC1.
    LS_APPROVER_DB-APPLEVEL_DESC2 = LT_APPROVER-APPLEVEL_DESC2.
    LS_APPROVER_DB-APPLEVEL_DESC3 = LT_APPROVER-APPLEVEL_DESC3.
    LS_APPROVER_DB-COUNTERSIGN = LT_APPROVER-COUNTERSIGN.
    LS_APPROVER_DB-RDATE = SY-DATUM.
    LS_APPROVER_DB-RTIME = SY-UZEIT.
    APPEND LS_APPROVER_DB TO LT_APPROVER_DB.
  ENDLOOP.
  IF ES_MSG-TYPE = 'E'.
    RETURN.
  ENDIF.

  SELECT SINGLE
      *
    INTO LS_APPR_USER
    FROM YBC_SWS_USER
   WHERE USERID = IS_APPROVER-APPROVERID.

  "CONCATENATE LS_APPR_USER-NAME1 LS_APPR_USER-NAME2 LS_APPR_USER-NAME3
  "INTO LV_USERNAME SEPARATED BY SPACE.
  LV_USERNAME = LS_APPR_USER-NAME1.
  CONDENSE: LV_USERNAME, LV_FWDS.

  "Remarks 更新，只更新还未处理的审批级别
  MESSAGE S041(YBC_SWS) WITH LV_USERNAME LV_FWDS INTO LV_REMARKS.

  "更新实际处理标识
  UPDATE YBC_SWS_APPROVER
      SET ACTION = 'FORWARD'
          COMMENTS = IS_APPROVER-COMMENTS
          REMARKS = LV_REMARKS
          ACTUALPROCESSED = 'X'
          CANCELED = 'X'
          PROCESSED = 'X'
          ADATE = SY-DATUM
          ATIME = SY-UZEIT
    WHERE REQUESTID = IS_APPROVER-REQUESTID
      AND APPROVERID = IS_APPROVER-APPROVERID
      AND APPLEVEL = IS_APPROVER-APPLEVEL
      AND VERSION = IS_APPROVER-VERSION
      AND PROCESSED = SPACE.

  MODIFY YBC_SWS_APPROVER FROM TABLE LT_APPROVER_DB.

  COMMIT WORK AND WAIT.
  IF SY-SUBRC <> 0.
    ROLLBACK WORK.
    RETURN.
  ENDIF.

  ES_MSG-TYPE = 'S'.
  MESSAGE S042(YBC_SWS) WITH IV_FORWARDAPPRS INTO ES_MSG-MESSAGE.
ENDFUNCTION.
